<template>
  <div class="category-container">
    <div class="top">通过“分类管理”设置店铺商品的展示分类，可以根据店铺运营需求更有效地展示商品，让顾客更方便地进行商品选择。</div>
    <el-row>
      <el-col :span="6"
              class="left">
        <div class="head">
          <div>分类</div>
          <div>
            <el-popover placement="bottom"
                        trigger="click"
                        v-model="visible">
              <el-input placeholder="请输入内容"
                        v-model="add_input"></el-input>
              <div style="text-align: right; margin-top: 20px">
                <el-button size="mini"
                           type="text"
                           @click="visible = false">取消</el-button>
                <el-button type="primary"
                           size="mini"
                           @click="addCategory">确定</el-button>
              </div>
              <el-button slot="reference"
                         size="small">添加</el-button>
            </el-popover>
          </div>
        </div>
        <div class="main"
             style="overflow-y:scroll">
          <div class="item"
               v-for="(item,index) in list"
               :class="item_index==index?'active':''">
            <div style="flex:1;cursor: pointer"
                 @click="handleItem(index,item.id)">{{item.title}}</div>
            <div>
              <el-popover placement="bottom"
                          trigger="click"
                          v-model="item.visible">
                <el-input placeholder="请输入内容"
                          v-model="item.title"></el-input>
                <div style="text-align: right; margin-top: 20px">
                  <el-button size="mini"
                             type="text"
                             @click="item.visible = false">取消</el-button>
                  <el-button type="primary"
                             size="mini"
                             @click="item.visible = false">确定</el-button>
                </div>

                <i class="el-icon-edit"
                   style="cursor: pointer"
                   slot="reference"></i>
              </el-popover>
              <i class="el-icon-delete"
                 style="cursor: pointer;margin-left:5px"></i>
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="18">
        <div style="overflow-y:scroll;overflow-x:hidden">
          <el-row class="right"
                  :gutter="20">
            <el-col :span="6"
                    v-for="item in table_data">
              <div class="item">
                <el-image :src="item.head"
                          fit="fill"></el-image>
                <div class="title"><el-checkbox v-model="item.checked">{{item.title}}</el-checkbox></div>
              </div>
            </el-col>
          </el-row>
        </div>
        <div>
          <el-button type="danger"
                     @click="handDelProduct">移除</el-button>
          <el-button @click="addProduct">添加</el-button>
        </div>
      </el-col>
    </el-row>

    <el-dialog title="收货地址"
               :visible.sync="dialogTableVisible">
      <el-table :data="product_list"
                height="400"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection"
                         width="55">
        </el-table-column>
        <el-table-column property="title"
                         label="标题"></el-table-column>
        <el-table-column property="price"
                         label="价格"
                         width="200"></el-table-column>
        <el-table-column property="stock"
                         label="库存"
                         width="150"></el-table-column>
      </el-table>
      <el-button>取消</el-button>
      <el-button type="primary"
                 @click="addProductData">确定</el-button>
    </el-dialog>

  </div>
</template>

<script>
import { getList, addData, getProduct, addProduct, delProduct } from '@/api/productCategory'
import { getList as getProductList } from '@/api/product'
export default {
  data () {
    return {
      item_index: 0,
      visible: false,
      add_input: '',
      list: [],
      table_data: [],
      dialogTableVisible: false,
      multipleSelection: [],
      product_list: []
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    fetchData () {
      getList().then(response => {
        response.data.data.forEach(element => {
          this.list.push({
            "id": element.id,
            "title": element.title,
            "visible": false
          })
        });
      })
    },
    // 点击左边子项目
    handleItem (index, id) {
      this.item_index = index
      getProduct({ category_id: id }).then(response => {
        if (response.code == 1000) {
          let data = response.data.data
          data.forEach((element, index) => {
            data[index].checked = false
          });
          this.table_data = data
        }
      });
    },
    addCategory () {
      addData({ title: this.add_input }).then(response => {
        if (response.code == 1000) {
          this.list.push({
            "id": response.data.data,
            "title": this.add_input,
            "visible": false
          })
          this.visible = false
        } else {
        }
      })
    },
    delCategory (id) {

    },
    // 选择好之后点确定
    addProductData () {
      let a = []
      if (this.multipleSelection.length == 0) {
        this.$message.error("请选择至少一个选项");
        return false
      }
      this.multipleSelection.forEach(element => {
        a.push(element.id)
      });
      let data = {
        category_id: this.list[this.item_index].id,
        product_id: a
      }
      addProduct(data).then(response => {
        if (response.code == 1000) {

        } else {

        }
      });
    },

    handDelProduct () {
      let a = [];
      this.table_data.forEach(element => {
        if (element.checked == true) {
          a.push(element.id)
        }
      });

      let data = {
        category_id: this.list[this.item_index].id,
        product_id: a
      }

      delProduct(data).then(response => {
        if (response.code == 200) {
          getProduct({ category_id: this.list[this.item_index].id }).then(response => {
            if (response.code == 1000) {
              let data = response.data.data
              data.forEach((element, index) => {
                data[index].checked = false
              });
              this.table_data = data
            }
          });
          this.$message({
            message: '移除成功',
            type: 'success'
          });
        } else {
          this.$message.error("移除失败");
        }
      })
    },
    addProduct () {
      this.dialogTableVisible = true
      this.product_list = []
      this.multipleSelection = []
      getProductList().then(response => {
        let a = [];
        this.table_data.forEach(element => {
          a.push(element.id)
        });
        response.data.data.forEach(element => {
          if (a.indexOf(element.id) == -1) {
            this.product_list.push(element)
          }
        });
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.category-container {
  width: 1200px;
  margin: 20px auto;
  margin-top: 50px;
  border-radius: 10px;
  background-color: #fff;
  color: #333;
  .top {
    padding: 30px;
    border-bottom: 1px solid #f5f5f5;
  }
  .left {
    padding: 20px;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .main {
      height: 500px;
      padding-top: 10px;
      .item {
        padding: 10px;
        margin-top: 15px;
        display: flex;
        justify-content: space-between;
      }
      .active {
        border-radius: 5px;
        background-color: #f5f5f5;
      }
    }
  }
  .right {
    height: 500px;
    border-left: 1px solid #f5f5f5;
    padding: 30px;
    .item {
      box-shadow: 0 2px 12px 0 #999;
      .head-img {
        border-bottom-right-radius: 5px;
        border-bottom-left-radius: 5px;
        height: 160px;
        width: 100%;
      }
      .title {
        padding: 10px 10px;
      }
    }
  }
}
</style>